import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class Main6 {
    //删除题目
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入删除的项目的id");
        int id = scanner.nextInt();
        //创建初始化数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/subjectlyw?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("1234");
        //构造连接
        Connection connection = dataSource.getConnection();
        //构造sql
        String sql = "delete from project where id = " + id;
        PreparedStatement statement = connection.prepareStatement(sql);
        //执行sql
        int flag = statement.executeUpdate(sql);
        if (flag != 0) {
            System.out.println("删除成功!");
        } else {
            System.out.println("删除失败,当前项目不存在");
        }
        statement.close();
        connection.close();
    }

}
